<template>
	<scroll-view scroll-x="true" @scroll="scroll" style="width: 100%;overflow: hidden;white-space: nowrap;">
		<view class="content">
		
			<view class="titles">
				选择原料库存
			</view>
				<!-- 原料库存 -->
			<uni-table ref="table" :loading="loading" border type="selection" emptyText="暂无更多数据"
				style="width: 100%; height: 100%; overflow: auto;" @selection-change="selectionChange">
				<uni-tr>
					<uni-th align="center" >原料编码</uni-th>
					<uni-th align="center">原料名称</uni-th>
					<uni-th align="center">在库数量</uni-th>
				


				</uni-tr>
				<uni-tr v-for="(item, index) in issueList">
					<uni-td >{{item.itemCode}}</uni-td>
					<uni-td >{{item.itemName}}</uni-td>
					<uni-td >{{item.quantityOnhand}}</uni-td>
				


				</uni-tr>
			</uni-table>

		</view>
		<uni-pagination style="width:100%" :show-icon="true" :pageSize="10" :total="total" @change="paginationChange" />
		<view style="margin-top: 10rpx;display: flex;justify-content: center;">
			<u-button type="primary" size="" style="width:200rpx;height:70rpx; margin: 0 10rpx;"
				@click="submit">确定</u-button>
			<u-button type="success" size="" style="width:200rpx;height:70rpx;margin: 0 10rpx;"
				@click="cancel">取消</u-button>
		</view>
		<u-toast ref="uToast" />
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				// 选中的表格数据
				currTable: [],
				// 发布工单
				issueList: [],
				// 选中的表格序号
				tableIndex: [],
				total: 0
			}
		},
		created() {
			this.$u.api.listWmstock({
				pageNum: 1,
				pageSize: 10,
			}).then(res => {
				console.log('发布工单', res)
				this.issueList = res.rows
				this.total = res.total;
			})
		},
		methods: {
			// 表格选中
			selectionChange(obj) {
				// this.tableIndex = obj.detail.index;

				// // 选中数据的索引
				// this.currTable = obj.detail.index && obj.detail.index.length > 0 ? this.
				// issueList[obj.detail
				// 		.index[
				// 			0]]:
				// 	null;
				let arr = []
				this.tableIndex = obj.detail.index;
				
				if (obj.detail.index && obj.detail.index.length > 0) {
					obj.detail.index.forEach(item => {
						arr.push(this.issueList[item])
					})
				}
				console.log('12312313', arr)
				this.currTable = arr;

			},
			// 确认表格的选中
			submit() {

				// if (this.tableIndex.length != 1) {
				// 	this.$refs.uToast.show({
				// 		title: '请选择一条数据',
				// 		type: 'error',
				// 	})
				// } else {
					
				// 	this.$emit('selectValue', this.currTable)
				// 	this.$emit('close-popup')
				// }
				if (this.currTable.length == 0) {
					this.$refs.uToast.show({
						title: '请至少选择一条数据',
						type: 'error',
					})
				} else {
				
					this.$emit('selectValue', this.currTable)
					this.cancel()
					this.$emit('close-popup')
				}
			},
			// 表格的取消
			cancel() {
				this.tableIndex = [];
				this.currTable = [];
				this.$emit('close-popup')
			},
			// 分页器页码变化
			paginationChange({
				type,
				current
			}) {

				this.$u.api.listWmstock({
					pageNum: current,
					pageSize: 10,
				}).then(res => {

					this.issueList = res.rows
					this.total = res.total;
				})
			}

		}
	}
</script>
<style scoped lang="scss">
	.content {

		padding: 20rpx;


		// width: 100%;

	}

	.titles {
		width: 100%;
		height: 50rpx;
		font-size: 30rpx;
		text-align: center;
		font-weight: 700;
	}
</style>